package com.xiuxia.usercenterbackend.common;

import lombok.Getter;

/**
 * @ClassName R
 * @Author CHENBO
 * @Date 2024/12/17 10:10
 */
@Getter
public class R<T> {
    private Integer code;
    private String message;
    private T data;

    private R(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> R<T> ok(T data) {
        return new R<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    public static <T> R<T> ok() {
        return ok(null);
    }
    // 失败时调用的方法
    public static <T> R<T> fail(ResponseCode responseCode) {
        return new R<>(responseCode.getCode(), responseCode.getMessage(), null);
    }

    // 失败时调用的方法，支持自定义错误信息
    public static <T> R<T> fail(ResponseCode responseCode, String customMessage) {
        return new R<>(responseCode.getCode(), customMessage, null);
    }
}
